// pages/device-detail/index.js
const api = require("../../utils/api.js")
const app = getApp()
import '../../plugins/weapp-cookie/weapp-cookie'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    device: null,
    dno: null,
    vipEndDays: 0,
    startStatus: 0,
    userInfo: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var dno = options.scene ? options.scene : null,
      that = this,
      userInfo = wx.getStorageSync('swUser')
    if (!userInfo) {
      wx.navigateTo({
        url: '/pages/login/index?dno=' + dno,
      })
      return false
    }

    that.setData({
      dno: dno
    })
    
    wx.requestWithCookie({
      url: api.getVIPRemainDaysUrl,
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {},
      dataType: 'json',
      success: function (res) {
        if (res.data.Code === 1) {
          var vipEndDays = res.data.Entity;
          app.globalData.vipEndDays = vipEndDays;
          that.setData({
            vipEndDays: vipEndDays
          })
          if(vipEndDays > 0) {
            that.startDevice()
            return true
          }
        }
        wx.showToast({
          icon: 'none',
          title: '非会员不能启动设备',
        })
        wx.navigateTo({
          url: '/pages/recharge/index',
        })
        that.setData({
          startStatus: 1
        })
        return false
      },
      fail: function () {

      }
    })
  },
  backIndex: function() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  startDevice: function () {
    var that = this,
      vipEndDays = this.data.vipEndDays,
      dno = that.data.dno
    
    if(!dno) {
      wx.showToast({
        icon: 'none',
        title: '请扫码后启动设备',
      })
      that.setData({
        startStatus: 1
      })
      return false
    }
    
    if (vipEndDays <= 0) {
      wx.showToast({
        icon: 'none',
        title: '非会员不能启动设备',
      })
      that.setData({
        startStatus: 1
      })
      return false
    } else {
      wx.request({
        url: api.startDeviceUrl,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        data: {
          DNO: dno
        },
        dataType: 'json',
        success: function (res) {
          // console.log(res)
          if (res.data.Code === 1) {
            wx.showToast({
              title: '启动成功',
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: res.data.Msg,
            })
            that.setData({
              startStatus: 1
            })
          }
        },
        fail: function (res) {
          wx.showToast({
            title: '启动失败',
          })
        },
        complete: function (res) {
          wx.hideLoading()
        }
      })
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})